package com.tdijoy.marqueen;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
 * Create by liangkun on 2018/11/8
 * Describe :
 */

public class DemoActivity extends AppCompatActivity {
    private View mCursor;// 光标
    private ImageView ivPerson;
    private ImageView ivCat;
    private ImageView ivPic;
//    private ImageView ivWomen;
    private RelativeLayout ivWomen;
    private Animation anim = new AlphaAnimation(1.0f, 0f);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acticity_demo);
        ivPerson = findViewById(R.id.iv_person);
        ivCat = findViewById(R.id.iv_cat);
        ivWomen = findViewById(R.id.iv_women);
        mCursor= findViewById(R.id.activity_dj_cursor);
        ivPic= findViewById(R.id.iv_set);
        ivPerson.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    /**
                     * 带闪动以及放大
                     */
                    v.setBackgroundResource(R.drawable.cursor_shadow);
                    ZoomViewUtils.zoomInAnim(v);

                } else {
                    v.setBackgroundResource(R.color.background_transparent);
                    ZoomViewUtils.zoomOutAnim(v);
                }
            }
        });
        ivCat.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    /**
                     * 带闪动以及放大
                     */
                    v.setBackgroundResource(R.drawable.cursor_shadow);
                    ZoomViewUtils.zoomInAnim(v);

                } else {
                    v.setBackgroundResource(R.color.background_transparent);
                    ZoomViewUtils.zoomOutAnim(v);
                }
            }
        });
        ivWomen.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    /**
                     * 带闪动以及放大
                     */
                    v.setBackgroundResource(R.drawable.cursor_shadow);
                    ZoomViewUtils.zoomInAnim(v);

                } else {
                    v.setBackgroundResource(R.color.background_transparent);
                    ZoomViewUtils.zoomOutAnim(v);
                }
            }
        });
        ivPic.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    locationCursor(ivPic);
                }else{
                    dismissCursor();
                }
            }
        });
    }

    /**
     * 显示光标
     */
    public void locationCursor(View view) {
        if (view == null) {
            return;
        }
        int[] locations = new int[2];
        view.getLocationOnScreen(locations);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                view.getWidth(), view.getHeight());
        params.topMargin = locations[1];
        params.leftMargin = locations[0];
        mCursor.setLayoutParams(params);
        mCursor.setVisibility(View.VISIBLE);
        // 光标的透明度渐变效果
        anim.setDuration(150);//设置动画持续时间
        anim.setRepeatCount(Animation.INFINITE);//设置重复次数
        anim.setRepeatMode(Animation.REVERSE);//设置反方向执行
        mCursor.startAnimation(anim);
    }
    /**
     * 隐藏光标
     */
    public void dismissCursor() {
        mCursor.clearAnimation();//解除动画
        mCursor.setVisibility(View.INVISIBLE);
    }
}
